import { prisma } from '../../utils/prisma'
import { getUserIdOrThrow } from '../../utils/guard'

export default eventHandler(async (event) => {
  getUserIdOrThrow(event)
  const id = Number(getRouterParam(event, 'id'))
  if (!Number.isFinite(id)) throw createError({ statusCode: 400, statusMessage: 'BAD_ID' })
  const item = await prisma.redemption.findUnique({ where: { id } })
  if (!item) throw createError({ statusCode: 404, statusMessage: 'NOT_FOUND' })
  await prisma.$transaction(async (tx) => {
    await tx.pointsLedger.deleteMany({ where: { sourceType: 'redemption', sourceId: id } })
    await tx.redemption.delete({ where: { id } })
  })
  return { ok: true }
})


